package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description DeviceDataServiceImpl
 * @Author loe
 * @Date 2024-07-24
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 获取物模型数据分页结果
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> getPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        String deviceName = deviceDataPageReqDto.getDeviceName();
        LocalDateTime startTime = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime endTime = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        Page<DeviceData> page = deviceDataMapper.pageSearch(deviceName, startTime, endTime);
        return PageResponse.of(page, DeviceDataVo.class);
    }

    /**
     * 按天统计查询指标数据
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, Long endTime, String functionId, Long startTime) {
        // 1. 转换时间格式
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        // 2.查询数据
        List<DeviceDataGraphVo> list1 = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, start, end);

        // 3.封装返回
        List<DeviceDataGraphVo> list2 = DeviceDataGraphVo.dayInstance(start);
        for (DeviceDataGraphVo deviceDataGraphVo : list2) {
            for (DeviceDataGraphVo dataGraphVo : list1) {
                if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())) {
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                    break;
                }
            }
        }
        return list2;
    }

    /**
     * 按周统计查询指标数据
     * @param iotId
     * @param endTime
     * @param functionId
     * @param startTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, Long endTime, String functionId, Long startTime) {
        // 1. 转换时间格式
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        // 2.查询数据
        List<DeviceDataGraphVo> list1 = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, start, end);

        // 3.封装返回
        List<DeviceDataGraphVo> list2 = DeviceDataGraphVo.weekInstance(start);
        for (DeviceDataGraphVo deviceDataGraphVo : list2) {
            for (DeviceDataGraphVo dataGraphVo : list1) {
                if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())) {
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                    break;
                }
            }
        }
        return list2;
    }
}
